package dbdao;


import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import com.lanou.horo.myapplication.dbdao.DaoSession;
import com.lanou.horo.myapplication.dbdao.PersonEntity;
import com.lanou.horo.myapplication.dbdao.PersonEntityDao;
import java.util.List;
import application.BasisApplication;
import de.greenrobot.dao.query.QueryBuilder;


/**
 * 帮助类
 **/
public class UserDBHepler {
    private static UserDBHepler helper;
    private static DaoSession daoSession;

    private UserDBHepler() {
    }

    public static UserDBHepler getInstance() {
        if (helper == null) {
            helper = new UserDBHepler();
            daoSession = BasisApplication.getDaoSession();
        }
        return helper;
    }

    // 保存数据
    public void saveUSERMSGList(List<PersonEntity> usermsg) {
        daoSession.getPersonEntityDao().insertInTx(usermsg);
    }

    //更新某个对象
    public void add(PersonEntity u) {
        daoSession.getPersonEntityDao().update(u);

    }

    // 查询数据
    public List<PersonEntity> queryDataAll() {
        return daoSession.getPersonEntityDao().loadAll();
    }

    // 根据用户名查询数据
    public List<PersonEntity> queryByName(String name) {
        QueryBuilder<PersonEntity> qb = daoSession.getPersonEntityDao().queryBuilder();
        qb.where(PersonEntityDao.Properties.Name.eq(name));
        return qb.list();
    }

    // 插入数据
    public long insert(ContentValues values) {
        SQLiteDatabase sq = daoSession.getPersonEntityDao().getDatabase();
        String tablename = daoSession.getPersonEntityDao().getTablename();
        return sq.insert(tablename, null, values);
    }

    //添加一个对象
    public void addUser(PersonEntity u) {
//		SQLiteDatabase sq = daoSession.getUSERMSGDao().getDatabase();
//		String tablename = daoSession.getUSERMSGDao().getTablename();
        daoSession.getPersonEntityDao().insert(u);

    }

    //按name返回某一个对象
    public PersonEntity getUsermsgByName(String name) {
        QueryBuilder<PersonEntity> qb = daoSession.getPersonEntityDao().queryBuilder();
        qb.where(PersonEntityDao.Properties.Name.eq(name));
        List<PersonEntity> list = qb.list();
        if (qb.list() != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    //按name返回某一个对象
    public PersonEntity getUsermsgByPhoneNumber(String phoneNumber) {
        QueryBuilder<PersonEntity> qb = daoSession.getPersonEntityDao().queryBuilder();
        qb.where(PersonEntityDao.Properties.PhoneNumber.eq(phoneNumber));
        List<PersonEntity> list = qb.list();
        if (qb.list() != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //按name更新数据
    public PersonEntity updateByName(String name) {
        QueryBuilder<PersonEntity> qb = daoSession.getPersonEntityDao().queryBuilder();
        qb.where(PersonEntityDao.Properties.Name.eq(name));
        return qb.list().get(0);
    }


    public void update(PersonEntity PersonEntity) {
        daoSession.getPersonEntityDao().insertOrReplace(PersonEntity);
    }

}